using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;

namespace SharePointSiteConfigurator
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("b7134eb3-13ab-480c-b6e5-355571925ec6")]
    public class SiteConfigurator_FeatureEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            ConfigureSiteProperties(properties);
        }



        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            DeconfigureSiteProperties(properties);
        }



        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }



        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
        private void ConfigureSiteProperties(SPFeatureReceiverProperties properties)
        {
            SPWeb site = (SPWeb)properties.Feature.Parent;
            string siteUrl = site.Url;
            XElement siteConfigurations = XElement.Load(properties.Definition.RootDirectory + @"\siteconfigurations.xml");
            string configuration = site.WebTemplate.ToUpper() + "#" + site.Configuration.ToString();


            SiteConfigurator siteConfigurator = new SiteConfigurator(siteUrl, siteConfigurations, configuration);
            //Diagnostic.Logging.LogMessage("SiteConfigurator_Log", "ConfigureSiteProperties", "Activating Feature");
            siteConfigurator.Run();
        }
        private void DeconfigureSiteProperties(SPFeatureReceiverProperties properties)
        {
            SPWeb site = (SPWeb)properties.Feature.Parent;
            string siteUrl = site.Url;

            XElement siteConfigurations = XElement.Load(properties.Definition.RootDirectory + @"\siteconfigurations.xml");
            string configuration = site.WebTemplate.ToUpper() + "#" + site.Configuration.ToString();

            SiteConfigurator siteConfigurator = new SiteConfigurator(siteUrl, siteConfigurations, configuration);
            //Diagnostic.Logging.LogMessage("SiteConfigurator_Log", "ConfigureSiteProperties", "Deactivating Feature");
            siteConfigurator.RunCleanUp();
        }
    }
}
